﻿using Common.BaseLibrary.Authorization;
using DepartmentWork.Migrator.Data;
using DepartmentWork.Model;
using DepartmentWork.Platform.Controllers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DepartmentWork.Platform.Areas.QueryArea.Controllers
{
    public class OperationWorkSelcetController : BaseController
    {
        //
        // GET: /QueryArea/OperationWorkSelcet/

        public ActionResult SelcetOperationWork()
        {


            if (!string.IsNullOrEmpty(CurrentUser.UserID))
            {
                #region 过段时间，可能就不认识你了，你是通过登录人的id 找出所属空间的全部成员id 然后通过查找运维关联人员表找出所有本团队人员负责的运维项目ID，然后去重复，然后根据ID查出所有的运维项目，前台只需要运维项目名称和ID

                List<PROJECT_MAINTENANCE_APPOPS_USER> MODEL = new List<PROJECT_MAINTENANCE_APPOPS_USER>();
                List<PROJECT_MAINTENANCE_APPOPS_USER> model = new List<PROJECT_MAINTENANCE_APPOPS_USER>();
                List<PROJECT_MAINTENANCE_APPOPS> Opertion = new List<PROJECT_MAINTENANCE_APPOPS>();
                List<ProjectManintenanceAppopsViewModel> opertionName = new List<ProjectManintenanceAppopsViewModel>();
                List<string> name = new List<string>();
                List<Guid> guid = new List<Guid>();
                Guid teamSpaceId = userTeamsapceRepository.GetModel(r => r.USER_ID == new Guid(CurrentUser.UserID)).FirstOrDefault().TEAMSPACE_ID;//登录人的团队空间ID
                List<UserInfoViewModel> userList = new List<UserInfoViewModel>();
                List<USER_TEAMSAPCE> userTeams = userTeamsapceRepository.GetModel(r => r.TEAMSPACE_ID == teamSpaceId).ToList();//该团队空间的所有人的用户集合
                userList.AddRange((from item in userTeams select new UserInfoViewModel() { UserId = item.USER_ID }).ToList());
                for (int i = 0; i < userList.Count(); i++)
                {
                    var id = userList[i].UserId;
                    model = projectMaintenanceAppopsUserRepository.GetModel(r => r.APPOPS_PRINCIPAL_USER_ID == id).ToList();
                    MODEL.AddRange(model);
                }
                if (MODEL.Count > 0)
                {
                    foreach (PROJECT_MAINTENANCE_APPOPS_USER No in MODEL)
                    {
                        guid.Add(No.APPOPS_APPOPS_ID);
                    }
                    List<Guid> nlist = guid.Distinct().ToList();
                    if (nlist.Count > 0)
                    {
                        foreach (Guid n in nlist)
                        {
                            PROJECT_MAINTENANCE_APPOPS appopsName = projectMaintenanceAppopsRepository.GetModel(r => r.APPOPS_ID == n).FirstOrDefault();
                            Opertion.Add(appopsName);
                        }
                    }
                    if (Opertion.Count > 0)
                    {
                        foreach (PROJECT_MAINTENANCE_APPOPS cont in Opertion)
                        {
                            ProjectManintenanceAppopsViewModel Cont = new ProjectManintenanceAppopsViewModel();
                            Cont.appops_aaa = cont.APPOPS_AAA;
                            Cont.appops_businesslinkman = cont.APPOPS_BUSINESSLINKMAN;
                            Cont.appops_businesstype = cont.APPOPS_BUSINESSTYPE;
                            Cont.appops_department = cont.APPOPS_DEPARTMENT;
                            Cont.appops_describe = cont.APPOPS_DESCRIBE;
                            Cont.appops_frequency = cont.APPOPS_FREQUENCY;
                            Cont.appops_id = cont.APPOPS_ID;
                            Cont.appops_name = cont.APPOPS_NAME;
                            Cont.appops_no = cont.APPOPS_NO;
                            Cont.appops_overtime = cont.APPOPS_OVERTIME;
                            Cont.appops_phone = cont.APPOPS_PHONE;
                            Cont.appops_quantity = cont.APPOPS_QUANTITY;
                            Cont.appops_rank = cont.APPOPS_RANK;
                            Cont.appops_relevancename = cont.APPOPS_RELEVANCENAME;
                            Cont.appops_relevanceway = cont.APPOPS_RELEVANCEWAY;
                            Cont.appops_state = cont.APPOPS_STATE;
                            Cont.appops_time = cont.APPOPS_TIME;
                            Cont.appops_workrank = cont.APPOPS_WORKRANK;
                            opertionName.Add(Cont);
                        }
                    }
                }
                TempData["operationName"] = opertionName;

                #endregion

                #region 获取该团队空间下的全部人员

                List<UserInfoViewModel> userList_list = new List<UserInfoViewModel>();
                List<USER_TEAMSAPCE> userTeams_list = userTeamsapceRepository.GetModel(r => r.TEAMSPACE_ID == teamSpaceId).ToList();
                userList_list.AddRange((from item in userTeams_list
                                        select new UserInfoViewModel()
                                        {
                                            UserId = item.USER_ID
                                        }).ToList());
                for (int i = 0; i < userList_list.Count(); i++)
                {
                    var id = userList_list[i].UserId;
                    userList_list[i].UserName =
                        userRepository.GetModel(r => r.USER_ID == id).FirstOrDefault().DISPLAY_NAME;
                }
                TempData["UserList"] = userList_list;

                #endregion
            }
            return View();
        }










        /// <summary>
        /// 运维项目查询AJAX
        /// </summary>
        /// <param name="NameN">运维ID</param>
        /// <param name="NameId">记录人ID</param>
        /// <param name="BTime">开始时间（以记录创建时间为准）</param>
        /// <param name="DTime">结束时间</param>
        /// <returns></returns>
        public JsonResult SelcetS(string NameN, string NameId, DateTime BTime, DateTime DTime)
        {

            List<PROJECT_MAINTENANCE_RECORD> RECORD = new List<PROJECT_MAINTENANCE_RECORD>();//运维记录（过程中中）
            List<ProjectManintenanceRecordViewModel> record = new List<ProjectManintenanceRecordViewModel>();//运维记录（返回前台）
            if (NameN != "0")//如果选择了项目
            {
               
                #region 全部记录人
                if (NameId == "0") //全部录入人
                {
                    Guid NameNid = new Guid(NameN);//运维ID
                  
                    RECORD = projectMaintenanceRecordRepository.GetModel(r => r.RECORD_OPERATION_ID == NameNid && r.RECORD_TIME >= BTime && r.RECORD_TIME <= DTime).ToList();

                }
                #endregion
                #region  选择了指定的录入人
                else //选择了指定的录入人
                {
                    Guid NameID = new Guid(NameId);//录入人ID
                    Guid NameNid = new Guid(NameN);//运维ID
                    RECORD = projectMaintenanceRecordRepository.GetModel(r => r.RECORD_OPERATION_ID == NameNid && r.RECORD_NAME_ID == NameID && r.RECORD_TIME >= BTime && r.RECORD_TIME <= DTime).ToList();
                }
                #endregion
            }
            else // 如果选择了全部项目
            {
                #region 全部负责人
                if (NameId == "0") //负责人：全部 
                {
                    RECORD = projectMaintenanceRecordRepository.GetModel(r =>r.RECORD_TIME >= BTime && r.RECORD_TIME <= DTime).ToList();
                }
                #endregion
                #region  选择了指定的负责人
                else //选择了指定的负责人
                {
                    Guid NameID = new Guid(NameId);//负责人ID
                    RECORD = projectMaintenanceRecordRepository.GetModel(r =>r.RECORD_NAME_ID==NameID&&r.RECORD_TIME >= BTime && r.RECORD_TIME <= DTime).ToList();

                }
                #endregion
            }


            #region 处理
            if (RECORD.Count > 0)
            {
                foreach (PROJECT_MAINTENANCE_RECORD x in RECORD.OrderByDescending(p=>p.RECORD_TIME))
                {
                    ProjectManintenanceRecordViewModel pl = new ProjectManintenanceRecordViewModel();
                    string name = projectMaintenanceAppopsRepository.Find(r => r.APPOPS_ID == x.RECORD_OPERATION_ID).APPOPS_NAME;
                    pl.record_accessory = x.RECORD_ACCESSORY;
                    pl.record_content = x.RECORD_CONTENT;
                    pl.record_id = x.RECORD_ID;
                    pl.record_name = x.RECORD_NAME;
                    pl.record_operation_id = x.RECORD_OPERATION_ID;
                    pl.record_operationtype = x.RECORD_OPERATIONTYPE;
                    pl.record_time = x.RECORD_TIME;
                    pl.record_time_selcet = x.RECORD_TIME.ToString("yyyy-MM-dd");
                    pl.record_operation_id = x.RECORD_OPERATION_ID;
                    pl.record_operation_name = name;
                    record.Add(pl);
                }
            }
            #endregion
            return Json(record);
        }






    }
}
